package poiManagement;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

//import model.POI;
import de.uni_karlsruhe.cm_tm.www.schema.poi.POI;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXReader {
    private SAXParserFactory factory;

    private SAXParser parser;

    private List<POI> poiList;

    private POI myPoi;

    private State state;

    private enum State {
        WAIT, NAME, CAT, LAT, LONG;
    }

    public List<POI> readAllPOIs(String url) {
        try {
            // initialize parser
            factory = SAXParserFactory.newInstance();
            parser = factory.newSAXParser();
            parser.parse(url, new MyHandler());
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return poiList;
    }

    private class MyHandler extends DefaultHandler {

        @Override
        public void startDocument() throws SAXException {
            System.out.println("SAX parser started - reading document...");
            state = State.WAIT;
            poiList = new LinkedList<POI>();
            super.startDocument();
        }

        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
            System.out.println(">Element " + qName + " found");
            //if (qName.equals("kcg:poi")) {
            if (qName.equals("tns:poi")) {
                System.out.println("\t Creating new POI...");
                myPoi = new POI();
                myPoi.setId(Integer.parseInt(attributes.getValue("id")));
            }            
            //if (qName.equals("kcg:name")) {
            if (qName.equals("tns:name")) {
                state = State.NAME;
            }
            //if (qName.equals("kcg:category")) {
            if (qName.equals("tns:category")) {
                state = State.CAT;
            }
            //if (qName.equals("kcg:latitude")) {
            if (qName.equals("tns:latitude")) {
                state = State.LAT;
            }
            //if (qName.equals("kcg:longitude")) {
            if (qName.equals("tns:longitude")) {
                state = State.LONG;
            }
            super.startElement(uri, localName, qName, attributes);
        }

        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            System.out.println(">>The State is " + state.toString());
            //according to the state we know which characters are read and we can save them accordingly 
            switch (state) {
            case NAME:
                myPoi.setName(new String(ch, start, length));
                System.out.println("\t Processing name...");
                break;
            case CAT:
                myPoi.setCategory(new String(ch, start, length));
                System.out.println("\t Processing name...");
                break;
            case LAT:
                myPoi.setLatitude(Float
                        .parseFloat(new String(ch, start, length)));
                System.out.println("\t Processing latitude...");
                break;
            case LONG:
                myPoi.setLongitude(Float.parseFloat(new String(ch, start,
                        length)));
                System.out.println("\t Processing longitude...");
                break;
            case WAIT:
                // do nothing
                break;
            }
            state = State.WAIT;
            super.characters(ch, start, length);
        }

        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            //if (qName.equals("kcg:poi")) {
        	if (qName.equals("tns:poi")) {
                poiList.add(myPoi);
            }
            super.endElement(uri, localName, qName);
        }

        @Override
        public void endDocument() throws SAXException {
            System.out.println(">Document ended!");
            super.endDocument();
        }
    }
}
